package com.company.cloud.base.modules.mgr.service.impl;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.base.modules.base.entity.BizTypeInfo;
import com.company.cloud.base.modules.base.service.BizTypeInfoService;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeSaveParam;
import com.company.cloud.base.modules.mgr.controller.req.BizCodeUpdateParam;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeSaveParam;
import com.company.cloud.base.modules.mgr.controller.req.BizTypeUpdateParam;
import com.company.cloud.base.modules.mgr.service.BizTypeCodeMgrService;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;

import cn.hutool.core.bean.BeanUtil;

@Service
public class BizTypeCodeMgrServiceImpl implements BizTypeCodeMgrService {

    @Autowired
    private BizTypeInfoService bizTypeInfoService;

    @Override
    public void save(BizTypeSaveParam params, LoginSimpleUserInfo userInfo) {
        BizTypeInfo info = bizTypeInfoService.getOne(new QueryWrapper<BizTypeInfo>()
                .eq("type_name", params.getTypeName()));

        if (info != null) {
            throw new BusinessException(BaseReturnEnum.DATA_EXISTS);
        }
        BizTypeInfo entity=new BizTypeInfo();
        BeanUtil.copyProperties(params, entity);
        entity.setTypeCode(UUID.randomUUID().toString().replaceAll("-", ""));
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        bizTypeInfoService.save(entity);
    }

    @Override
    public void update(BizTypeUpdateParam params, LoginSimpleUserInfo userInfo) {
        BizTypeInfo entity= bizTypeInfoService.getById(params.getTypeName());
        if (entity == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BeanUtil.copyProperties(params, entity);
        entity.addEditParam(userInfo);
        bizTypeInfoService.updateById(entity);
    }

    @Override
    public void save(BizCodeSaveParam params, LoginSimpleUserInfo userInfo) {
        BizTypeInfo info = bizTypeInfoService.getOne(new QueryWrapper<BizTypeInfo>()
                .eq("type_code", params.getTypeCode())
                .eq("vname", params.getVname())
        );
        if (info != null) {
            throw new BusinessException(BaseReturnEnum.DATA_EXISTS);
        }
        BizTypeInfo entity=new BizTypeInfo();
        BeanUtil.copyProperties(params, entity);
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        bizTypeInfoService.save(entity);
    }

    @Override
    public void update(BizCodeUpdateParam params, LoginSimpleUserInfo userInfo) {
        BizTypeInfo entity= bizTypeInfoService.getById(params.getId());
        if (entity == null) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BeanUtil.copyProperties(params, entity);
        entity.addEditParam(userInfo);
        bizTypeInfoService.updateById(entity);
    }

}
